﻿/*
 * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：研发部
 * 日 期：2017.03.16
 * 描 述：手风琴风格皮肤	
 */
var pageid = 'AdminProjectManage';
var userId;
var ProjectId = request("ProjectId");
var verify;
var toDo;
var processtoDo;
var toTemporaryTasks;
//跳转页面信息
var ToifameName = request("ifameName");
//流程信息
var processId = request("processId");
var taskId = request("taskId");
var title = unescape(request('title'));
var taskName = unescape(request('taskName'));
var taskType = request("taskType");

var bootstrap = function ($, learun) {
    "use strict";
    // 菜单操作
    var meuns = {
        init: function (parentId) {
            this.load(parentId);
            this.bind();
            ///获取用户待办事项
        },
        load: function (parentId) {
            var modulesTree = learun.clientdata.get(['modulesTree']);

            var modules = modulesTree[parentId] || [];
            var $firstmenus = $('<ul class="lr-first-menu-list"></ul>');
            for (var i = 0, l = modules.length; i < l; i++) {
                var item = modules[i];
                if (item.F_IsMenu == 1) {
                    var $firstMenuItem = $('<li></li>');
                    if (!!item.F_Description) {
                        $firstMenuItem.attr('title', item.F_Description);
                    }
                    var secondModules = modulesTree[item.F_ModuleId] || [];
                    var menuItemHtml = '<a id="' + item.F_ModuleId + '" href="javascript:void(0);" class="lr-menu-item">';
                    menuItemHtml += '<i class="' + item.F_Icon + ' lr-menu-item-icon"></i>';
                    menuItemHtml += '<span class="lr-menu-item-text">' + item.F_FullName + '</span>';
                    if (secondModules != "") {
                        menuItemHtml += '<span class="lr-menu-item-arrow"><i class="fa fa-angle-left"></i></span></a>';
                    }
                    $firstMenuItem.append(menuItemHtml);
                    // 第二级菜单
                    // var secondModules = modulesTree[item.F_ModuleId] || [];
                    var $secondMenus = $('<ul class="lr-second-menu-list"></ul>');
                    var secondMenuHad = false;
                    for (var j = 0, sl = secondModules.length; j < sl; j++) {
                        var secondItem = secondModules[j];
                        if (secondItem.F_IsMenu == 1) {
                            secondMenuHad = true;
                            var $secondMenuItem = $('<li></li>');
                            if (!!secondItem.F_Description) {
                                $secondMenuItem.attr('title', secondItem.F_Description);
                            }
                            var secondItemHtml = '<a id="' + secondItem.F_ModuleId + '" href="javascript:void(0);" class="lr-menu-item" >';
                            secondItemHtml += '<i class="' + secondItem.F_Icon + ' lr-menu-item-icon"></i>';
                            secondItemHtml += '<span class="lr-menu-item-text">' + secondItem.F_FullName + '</span>';
                            secondItemHtml += '</a>';

                            $secondMenuItem.append(secondItemHtml);
                            // 第三级菜单
                            var threeModules = modulesTree[secondItem.F_ModuleId] || [];
                            var $threeMenus = $('<ul class="lr-three-menu-list"></ul>');
                            var threeMenuHad = false;
                            for (var m = 0, tl = threeModules.length; m < tl; m++) {
                                var threeItem = threeModules[m];
                                if (threeItem.F_IsMenu == 1) {
                                    threeMenuHad = true;
                                    var $threeMenuItem = $('<li></li>');
                                    if (!!threeItem.F_Description) {
                                        $threeMenuItem.attr('title', threeItem.F_Description);
                                    }
                                    $threeMenuItem.attr('title', threeItem.F_FullName);
                                    var threeItemHtml = '<a id="' + threeItem.F_ModuleId + '" href="javascript:void(0);" class="lr-menu-item" >';
                                    threeItemHtml += '<i class="' + threeItem.F_Icon + ' lr-menu-item-icon"></i>';
                                    threeItemHtml += '<span class="lr-menu-item-text">' + threeItem.F_FullName + '</span>';
                                    threeItemHtml += '</a>';
                                    $threeMenuItem.append(threeItemHtml);
                                    // 第四级菜单
                                    var fourModules = modulesTree[threeItem.F_ModuleId] || [];
                                    var $fourMenus = $('<ul class="lr-four-menu-list"></ul>');
                                    var fourMenuHad = false;
                                    for (var n = 0, pl = fourModules.length; n < pl; n++) {
                                        var fourItem = fourModules[n];
                                        if (fourItem.F_IsMenu == 1) {
                                            fourMenuHad = true;
                                            var $fourMenuItem = $('<li></li>');
                                            $fourMenuItem.attr('title', fourItem.F_FullName);
                                            var fourItemHtml = '<a id="' + fourItem.F_ModuleId + '" href="javascript:void(0);" class="lr-menu-item" >';
                                            fourItemHtml += '<i class="' + fourItem.F_Icon + ' lr-menu-item-icon"></i>';
                                            fourItemHtml += '<span class="lr-menu-item-text">' + fourItem.F_FullName + '</span>';
                                            fourItemHtml += '</a>';
                                            $fourMenuItem.append(fourItemHtml);
                                            $fourMenus.append($fourMenuItem);
                                        }
                                    }
                                    if (fourMenuHad) {
                                        $threeMenuItem.addClass('lr-meun-had');
                                        $threeMenuItem.find('a').append('<span class="lr-menu-item-arrow"><i class="fa fa-angle-left"></i></span>');
                                        $threeMenuItem.append($fourMenus);
                                    }
                                    $threeMenus.append($threeMenuItem);
                                }
                            }
                            if (threeMenuHad) {
                                $secondMenuItem.addClass('lr-meun-had');
                                $secondMenuItem.find('a').append('<span class="lr-menu-item-arrow"><i class="fa fa-angle-left"></i></span>');
                                $secondMenuItem.append($threeMenus);
                            }
                            $secondMenus.append($secondMenuItem);
                        }
                    }
                    if (secondMenuHad) {
                        $firstMenuItem.append($secondMenus);
                    }
                    $firstmenus.append($firstMenuItem);
                }
            }
            $('#lr_frame_menu').html($firstmenus);


            // 语言包翻译
            $('.lr-menu-item-text').each(function () {
                var $this = $(this);
                var text = $this.text();
                learun.language.get(text, function (text) {
                    $this.text(text);
                    $this.parent().parent().attr('title', text);
                });
            });
        },
        bind: function () {
            $("#lr_frame_menu").lrscroll();



            $('#lr_frame_menu a').hover(function () {
                if ($('body').hasClass('lr-menu-closed')) {
                    var id = $(this).attr('id');
                    var text = $('#' + id + '>span').text();
                    layer.tips(text, $(this));
                }
            }, function () {
                if ($('body').hasClass('lr-menu-closed')) {
                    layer.closeAll('tips');
                }
            });

            $('.lr-frame-personCenter .dropdown-toggle').hover(function () {
                if ($('body').hasClass('lr-menu-closed')) {
                    var text = $(this).text();
                    layer.tips(text, $(this));
                }
            }, function () {
                if ($('body').hasClass('lr-menu-closed')) {
                    layer.closeAll('tips');
                }
            });


            // 添加点击事件
            $('#lr_frame_menu a').on('click', function () {
                var $obj = $(this);
                var id = $obj.attr('id');
                var _module = learun.clientdata.get(['modulesMap', id]);
                switch (_module.F_Target) {
                    case 'iframe':// 窗口
                        if (learun.validator.isNotNull(_module.F_UrlAddress).code) {
                            var list = $("#lr_frame_tabs_ul li");
                            var len = list.length;
                            //if (len >= 8) {
                            //  var id = $(list[1]).attr('id').replace('lr_tab_', '');
                            //learun.frameTab.close(id);
                            // }
                            //项目平台链接注入ProjectId参数
                            if (!!ProjectId && !!_module.F_UrlAddress && _module.F_UrlAddress.indexOf("ProjectId=") == -1) {
                                if (_module.F_UrlAddress.indexOf('?') == -1) {
                                    _module.F_UrlAddress = _module.F_UrlAddress + "?ProjectId=" + ProjectId;
                                } else {
                                    _module.F_UrlAddress = _module.F_UrlAddress + "&ProjectId=" + ProjectId;
                                }
                            }
                            learun.frameTab.open(_module);
                        }
                        break;
                    case 'expand':// 打开子菜单
                        var $ul = $obj.next();
                        if ($ul.is(':visible')) {
                            $ul.slideUp(500, function () {
                                $obj.removeClass('open');
                            });
                        }
                        else {
                            if ($ul.hasClass('lr-second-menu-list')) {
                                $('#lr_frame_menu .lr-second-menu-list').slideUp(300, function () {
                                    $(this).prev().removeClass('open');
                                });
                            }
                            else if ($ul.hasClass('lr-three-menu-list')) {
                                $('#lr_frame_menu .lr-three-menu-list').slideUp(300, function () {
                                    $(this).prev().removeClass('open');
                                });
                            }
                            else {
                                $ul.parents('.lr-three-menu-list').find('.lr-four-menu-list').slideUp(300, function () {
                                    $(this).prev().removeClass('open');
                                });
                            }
                            $ul.slideDown(300, function () {
                                $obj.addClass('open');
                            });
                        }
                        break;
                }
            });
        }
    };

    // 项目平台一级菜单ID
    var parentId = '2c275558-2627-40a5-b020-d27c15fc14e3';
    meuns.init(parentId);

    //【消息提醒】
    userId = $("#Hidden_UserId").val();
    //创建集线器代理
    var chat = $.connection.chatHub;
    //定义供服务端调用的方法，发送消息 lr_im_msg
    chat.client.sendMsg = function (messages) {
        if (messages.Count > 0) {
            $("#lr-im-alarm-model").css("display", "block");
            //if (messages.Count > 99) {
            //    $("#MsgCount").text("99+");
            //    $("#MsgCount").css("width", "22px");
            //} else {
            //    $("#MsgCount").text(messages.Count);
            //}
            $("#MsgContent").html('');
            //var data = eval("(" + messages.Json + ")");
            var html = "";
            var data = JSON.parse(messages.Json);
            for (var i = 0; i < data.length; i++) {
                html += `<li class="list-group-item" id="${data[i].F_MsgId}" onclick="openMsg('${data[i].F_Content}','${data[i].F_MsgId}','${data[i].F_IsRead}')">`;
                if (data[i].Title.length >= 20) {
                    html += '<span class="">&nbsp;' + data[i].Title.substring(0, 20) + '...</span >';
                } else {
                    html += '<span class="">&nbsp;' + data[i].Title + '</span >';
                }
                //html += '<span class="" style="overflow: hidden;text-overflow:ellipsis;white-space: nowrap;" >' + data[i].Title + '</span >';
                //html += '<span class="warning-date"></span>';
                html += '<span class="warning-content">' + learun.formatDate(data[i].F_CreateDate, "yyyy-MM-dd hh:mm:ss") + '&nbsp;&nbsp;&nbsp;&nbsp;发送人:' + data[i].F_SendUserId + '</span>';
                html += '</li>'
            }
            $("#MsgContent").append(html);
            $("#lr-im-alarm-model").animate({ bottom: 0 }, 1000);
        } else {
            $("#lr-im-alarm-model").css("display", "none");
        }
    }
    //定义供服务端调用的方法，发送消息 流程任务
    chat.client.broadcastMessage = function (messages) {
        if (messages.Count > 0) {
            $("#process").css("display", "block");
            //if (messages.Count > 99) {
            //    $("#processCount").text("99+");
            //    $("#processCount").css("width", "22px");
            //} else {
            //    $("#processCount").text(messages.Count);
            //}
            $("#processContent").html('');
            var data = eval("(" + messages.Json + ")");
            var html = "";
            for (var i = 0; i < data.length; i++) {
                html += '<li class="list-group-item" onclick="verify(&quot;' + data[i].F_Id + '&quot;,&quot;' + data[i].F_TaskId + '&quot;,&quot;' + data[i].F_Title + '&quot;,&quot;' + data[i].F_SchemeName + '&quot;,&quot;' + data[i].F_TaskName + '&quot;,' + data[i].F_TaskType + ',&quot;' + data[i].F_SchemeCode + '&quot;)">';
                if (data[i].F_TaskType == 3) {
                    html += '<span class="warning-type" >【加签】' + data[i].F_SchemeName + '</span>';
                }
                else {
                    html += '<span class="warning-type" >' + data[i].F_SchemeName + '</span >';
                }
                html += '<span class="warning-date">' + learun.formatDate(data[i].F_CreateDate, "yyyy-MM-dd hh:mm:ss") + '</span>';
                if (data[i].F_SchemeName != data[i].F_Title) {
                    var name = data[i].F_TaskName + '(' + data[i].F_Title + ')';
                    if (name.length > 21) {
                        name = name.substring(0, 20) + '......';
                    }
                    html += '<span title="' + data[i].F_TaskName + '(' + data[i].F_Title + ')' + '" class="warning-content">' + name + '</span>';
                }
                else {
                    var name = data[i].F_TaskName;
                    if (name.length > 20) {
                        name = data[i].F_TaskName.substring(0, 20) + '...';
                    }
                    html += '<span title="' + data[i].F_TaskName + '" class="warning-content">' + name + '</span>';
                }
                html += '</li>'
            }
            $("#processContent").append(html);
            $("#process").animate({ bottom: '0px' }, 1000);
        } else {
            $("#process").css("display", "none");
        }
    };
    $.connection.hub.qs = { 'userId': $("#Hidden_UserId").val(), 'ProjectId': ProjectId };
    //默认加载执行
    function init() {
        chat.server.send(userId, ProjectId);
    }
    //AgainCreateFlow SignAuditFlow AuditFlow SignFlowNew 审批后台需要推送即时消息方法。
    $.connection.hub.start().done(init);//开启客户端SignalR，并首次运行init
    //关闭消息弹框
    $("#box-head-close").click(function () {
        $("#lr-im-alarm-model").animate({ bottom: '-800px' }, 1000);
    });
    //关闭流程弹框
    $("#processclose").click(function () {
        $("#process").animate({ bottom: '-500px' }, 1000);
    });
    //跳转到对应的处理页面
    verify = function (processId, taskId, title, schemeName, taskName, taskType) {

        var UpdateStatus = false;
        $.ajax({
            type: 'GET',
            url: top.$.rootUrl + '/LR_NewWorkFlow/NWFProcess/UpdateLook?taskId=' + taskId + '&UserId=' + $("#Hidden_UserId").val() + '&isLook=1',
            data: null,
            async: false,
            success: function (data) {
                var dataInfo = JSON.parse(data);
                if (dataInfo["code"] != 200) {
                    UpdateStatus = true;
                }
                //else {
                //    //刷新页面消息提醒内容
                //    chat.server.send(userId);
                //}
            },
            error: function (msg) { }, complete: function (data) {
            }
        });
        if (UpdateStatus) {
            learun.alert.warning("状态更新失败!");
            return;
        }
        if (schemeName != title && title) {
            title = schemeName + "(" + title + ")";
        }
        else {
            title = schemeName;
        }
        //1审批2传阅3加签4子流程5重新创建[ShortcutEntrance(快捷入口)]；
        switch (taskType) {
            case 1:// 审批
                learun.frameTab.open({ F_ModuleId: taskId, F_Icon: 'fa magic', F_FullName: '审批-' + title + '/' + taskName, F_UrlAddress: '/LR_NewWorkFlow/NWFProcess/NWFContainerForm?tabIframeId=' + taskId + '&type=audit' + "&processId=" + processId + "&taskId=" + taskId + "&ShortcutEntrance=1" });
                break;
            case 2:// 传阅
                learun.frameTab.open({ F_ModuleId: taskId, F_Icon: 'fa magic', F_FullName: '查阅-' + title + '/' + taskName, F_UrlAddress: '/LR_NewWorkFlow/NWFProcess/NWFContainerForm?tabIframeId=' + taskId + '&type=refer' + "&processId=" + processId + "&taskId=" + taskId + "&ShortcutEntrance=1" });
                break;
            case 3:// 加签
                learun.frameTab.open({ F_ModuleId: taskId, F_Icon: 'fa magic', F_FullName: '加签审核-' + title + '/' + taskName, F_UrlAddress: '/LR_NewWorkFlow/NWFProcess/NWFContainerForm?tabIframeId=' + taskId + '&type=signAudit' + "&processId=" + processId + "&taskId=" + taskId + "&ShortcutEntrance=1" });
                break;
            case 4:// 子流程
                learun.frameTab.open({ F_ModuleId: taskId, F_Icon: 'fa magic', F_FullName: '子流程-' + title + '/' + taskName, F_UrlAddress: '/LR_NewWorkFlow/NWFProcess/NWFContainerForm?tabIframeId=' + taskId + '&type=chlid' + "&processId=" + processId + "&taskId=" + taskId + "&ShortcutEntrance=1" });
                break;
            case 5:// 重新创建
                learun.frameTab.open({ F_ModuleId: processId, F_Icon: 'fa magic', F_FullName: '重新发起-' + title, F_UrlAddress: '/LR_NewWorkFlow/NWFProcess/NWFContainerForm?processId=' + processId + '&tabIframeId=' + processId + '&type=againCreate' + "&ShortcutEntrance=1" });
                break;
            case 6:// 重新创建
                learun.frameTab.open({ F_ModuleId: taskId, F_Icon: 'fa magic', F_FullName: '子流程-' + title + '/' + taskName, F_UrlAddress: '/LR_NewWorkFlow/NWFProcess/NWFContainerForm?tabIframeId=' + taskId + '&type=againChild' + "&processId=" + processId + "&taskId=" + taskId + "&ShortcutEntrance=1" });
                break;
        }
        //点击之后缩回消息提醒框
        //$("#process").animate({ bottom: '-500px' }, 1000);
    }
    processtoDo = function () {
        if ($("#lr_frame_tabs_ul").find("li[id='lr_tab_56ce34c2-882e-47d1-b12d-5036e3b79fcf']").length <= 0) {
            learun.frameTab.open({ F_ModuleId: '56ce34c2-882e-47d1-b12d-5036e3b79fcf', F_Icon: 'fa fa-user', F_FullName: '流程任务', F_UrlAddress: '/LR_NewWorkFlow/NWFProcess/Index' });
        }
    }
    toDo = function () {
        learun.frameTab.open({ F_Icon: 'fa magic', F_FullName: '消息中心', F_UrlAddress: '/LR_IM/IMMsg/Index' });
    };
    $("#childsystem-ul li").click(function () {
        $("#childsystem-ul li").removeClass("active");
        $(this).addClass("active");
        var topmenuid = $(this).attr("lang");
        meuns.init(topmenuid);
    });
};


function MenuClick() {
    //var $body = $('body');
    //if ($body.hasClass('lr-menu-closed')) {
    //    $body.removeClass('lr-menu-closed');
    //}
    //else {
    //    $body.addClass('lr-menu-closed');
    //}
    if (!$(".lr-frame-menu").is(":hidden")) {
        $(".lr-frame-menu").hide();
        $(".lr-frame-main").css("padding", "56px 0px 0px 0px");
    }
    else {
        $(".lr-frame-menu").show();
        $(".lr-frame-main").css("padding", "56px 0px 0px 200px");
    }
};

function openMsg(content, id, isRead) {
    $("#" + id).remove();
    if ($("#MsgContent").html() == '') {
        $("#lr-im-alarm-model").animate({ bottom: '-500px' }, 1000);
    }
    top.layer.open({
        type: 1,
        content: `<div style="font-size:18px;margin:20px 20px 0px 20px">${content}</div>`,
        area: [1000 + 'px', 600 + 'px'],
        title: '内容详情',
        maxmin: true,
        success: function (layero, index) {
            $.post(top.$.rootUrl + '/LR_IM/IMMsg/UpdateIsread', { msgId: id, isRead: "1" }, function (data) {
                let str = window.parent.$("#getMsgCount").text();
                let str1 = str.substring(1, str.length - 1);
                if (str1 && isRead == 0) {
                    if ((str1 - 1) == 0) {
                        window.parent.$("#getMsgCount").text("")
                    } else {
                        window.parent.$("#getMsgCount").text("(" + (str1 - 1) + ")")
                    }
                }
            })
        },
    });
}